using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.GamerServices;

namespace Elementum
{
    class OPeningStage : Stage
    {
        Image _openImage2;
        Image _openImage1;
        int tempCount = 0;

        public OPeningStage(Game game)
            : base(game)
        {
        }

        public override void InitializeScenes()
        {
           
            _lockTouch = false;
            camera = new Camera2D(this.Game);

            Animation open1 =
            new Animation("images/system/black", new Point(800, 480));
            Image openImage1 = new Image(open1);
            openImage1.Position = new Vector2(400, 240);
            PositionModifier m = new PositionModifier(openImage1.Position, openImage1.Position + new Vector2(0, 0), 0, 240);
            ModifierContain mc = new ModifierContain(openImage1);

            openImage1.ModifierContainer = mc;
            m.ModifyDoneAction += Play;
            mc.AddModifier(m);

            _controls.Add(openImage1);

            Animation openText = new Animation("images/opening/text",new Rectangle(2, 401, 532, 24), new Point(532, 24));
            Image openTextImage = new Image(openText);
            openTextImage.Position = new Vector2(366, 400);
            PositionModifier p_text1 = new PositionModifier(openTextImage.Position, openTextImage.Position + new Vector2(0, -50), 120, 240);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 120, 150);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 270, 300);
            ModifierContain mc_text1 = new ModifierContain(openTextImage);
            openTextImage.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage);

            _song = MusicManager.LoadSong("Music/opemmusic");
            MediaPlayer.Play(_song);
            MediaPlayer.IsRepeating = true;
            if (MediaPlayer.Volume > 0.4f)
            MediaPlayer.Volume = 0.4f;
        }
        
        public void Play()
        {
            Animation open1 =
                new Animation("images/system/black", new Point(800, 480));
            Image openImage1 = new Image(open1);
            openImage1.Position = new Vector2(400, 240);
            PositionModifier m = new PositionModifier(openImage1.Position, openImage1.Position + new Vector2(0, 0), 0, 120);
            ModifierContain mc = new ModifierContain(openImage1);
            openImage1.ModifierContainer = mc;
            m.ModifyDoneAction += Play1;
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(m);

            _controls.Add(openImage1);

            Animation openText_1 = new Animation("images/opening/text", new Rectangle(2, 375, 631, 24), new Point(631, 24));
            Image openTextImage_1 = new Image(openText_1);
            openTextImage_1.Position = new Vector2(400, 400);
            PositionModifier p_text1 = new PositionModifier(openTextImage_1.Position, openTextImage_1.Position + new Vector2(0, -50), 0, 120);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 150, 180);
            ModifierContain mc_text1 = new ModifierContain(openTextImage_1);
            openTextImage_1.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage_1);
        }

        public void Play1()
        {
            Animation open1 =
                new Animation("images/opening/city");
            Image openImage1 = new Image(open1);
            openImage1.Position = new Vector2(400 - 650, 200);
            PositionModifier m = new PositionModifier(openImage1.Position, openImage1.Position + new Vector2(1300, 40), 0, 240);
            AlphaModifier a = new AlphaModifier(0, 1, 1, 60);
            AlphaModifier a2 = new AlphaModifier(1, 0, 210, 240);
            ModifierContain mc = new ModifierContain(openImage1);
            openImage1.ModifierContainer = mc;
            m.ModifyDoneAction += Play2;
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m);

            _controls.Add(openImage1);

            Animation openText1 = new Animation("images/opening/text", new Rectangle(2, 197, 522, 90), new Point(522, 90));
            Image openTextImage1 = new Image(openText1);
            openTextImage1.Position = new Vector2(300, 100);
            PositionModifier p_text1 = new PositionModifier(openTextImage1.Position, openTextImage1.Position + new Vector2(80, 40), 0, 240);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 60);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 210, 240);
            ModifierContain mc_text1 = new ModifierContain(openTextImage1);
            openTextImage1.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage1);
        }

        public void Play2()
        {
            Animation open2 = new Animation("images/opening/s8");
            Image openImage2 = new Image(open2);
            openImage2.Position = new Vector2(270, 110);
            PositionModifier m = new PositionModifier(openImage2.Position, openImage2.Position + new Vector2(20, -100), 0, 120);
            AlphaModifier a = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a2 = new AlphaModifier(1, 0, 90, 120);
            ModifierContain mc = new ModifierContain(openImage2);
            openImage2.ModifierContainer = mc;
            m.ModifyDoneAction += Play3;
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m);
            _controls.Add(openImage2);

            Animation openText2 = new Animation("images/opening/text", new Rectangle(666, 99, 286, 38), new Point(286, 38));
            Image openTextImage2 = new Image(openText2);
            openTextImage2.Scale = 1.2f;
            openTextImage2.Position = new Vector2(220, 100);
            PositionModifier p_text1 = new PositionModifier(openTextImage2.Position, openTextImage2.Position + new Vector2(80, 0), 0, 120);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 90, 120);
            ModifierContain mc_text1 = new ModifierContain(openTextImage2);
            openTextImage2.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage2);
        }

        public void Play3()
        {
            Animation open2 = new Animation("images/opening/open", new Rectangle(2, 57, 162, 146), new Point(162, 146));

            Image openImage2 = new Image(open2);
            openImage2.Position = new Vector2(400, 240);
            PositionModifier m = new PositionModifier(openImage2.Position, openImage2.Position + new Vector2(0, 50), 0, 120);
            AlphaModifier a = new AlphaModifier(0.8f, 1, 1, 45);
            AlphaModifier a2 = new AlphaModifier(1, 0, 90, 120);
            ModifierContain mc = new ModifierContain(openImage2);
            openImage2.ModifierContainer = mc;
            m.ModifyDoneAction += Play4;
            mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m);
            _controls.Add(openImage2);

            Animation open1 =
                new Animation("images/opening/open", new Rectangle(278, 2, 162, 146), new Point(162, 146));
            Image openImage1 = new Image(open1);
            openImage1.Position = new Vector2(400, 240);
            PositionModifier m1 = new PositionModifier(openImage1.Position, openImage1.Position + new Vector2(0, 50), 0, 120);
            a = new AlphaModifier(0, 1, 1, 45);
            a2 = new AlphaModifier(1, 0, 90, 120);
            mc = new ModifierContain(openImage1);
            openImage1.ModifierContainer = mc;
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m1);

            _controls.Add(openImage1);

            Animation openText3 = new Animation("images/opening/text", new Rectangle(526, 197, 468, 30), new Point(468, 30));
            Image openTextImage3 = new Image(openText3);
            openTextImage3.Position = new Vector2(400, 100);
            PositionModifier p_text1 = new PositionModifier(openTextImage3.Position, openTextImage3.Position + new Vector2(0, 0), 0, 120);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 90, 120);
            ModifierContain mc_text1 = new ModifierContain(openTextImage3);
            openTextImage3.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage3);

        }

        public void Play4()
        {
            Animation open2 = new Animation("images/opening/cityGood");
            Image openImage2 = new Image(open2);
            openImage2.Position = new Vector2(350, 300);
            PositionModifier m = new PositionModifier(openImage2.Position, openImage2.Position + new Vector2(100, -100), 0, 180);
            AlphaModifier a = new AlphaModifier(0, 1, 1, 60);
            AlphaModifier a2 = new AlphaModifier(1, 0, 150, 180);
            ModifierContain mc = new ModifierContain(openImage2);
            openImage2.ModifierContainer = mc;
            m.ModifyDoneAction += Play5;
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m);
            _controls.Add(openImage2);

            Animation openText4_1 = new Animation("images/opening/text", new Rectangle(2, 99, 662, 96), new Point(662, 96));
            Image openTextImage4_1 = new Image(openText4_1);
            openTextImage4_1.Position = new Vector2(400, 100);
            PositionModifier p_text1 = new PositionModifier(openTextImage4_1.Position, openTextImage4_1.Position + new Vector2(0, 0), 0, 180);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 150, 180);
            ModifierContain mc_text1 = new ModifierContain(openTextImage4_1);
            openTextImage4_1.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage4_1);
        }

        public void Play5()
        {
            Animation open2 = new Animation("images/opening/cityBad");
            Image openImage2 = new Image(open2);
            openImage2.Position = new Vector2(400, 350);
            PositionModifier m = new PositionModifier(openImage2.Position, openImage2.Position + new Vector2(-100, 50), 0, 180);
            AlphaModifier a = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a2 = new AlphaModifier(1, 0, 150, 180);
            ModifierContain mc = new ModifierContain(openImage2);
            openImage2.ModifierContainer = mc;
            m.ModifyDoneAction += Play6;
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m);
            _controls.Add(openImage2);

            Animation openText5 = new Animation("images/opening/text", new Rectangle(2, 2, 768, 95), new Point(768, 95));
            Image openTextImage5 = new Image(openText5);
            openTextImage5.Position = new Vector2(400, 400);
            PositionModifier p_text1 = new PositionModifier(openTextImage5.Position, openTextImage5.Position + new Vector2(0, 0), 0, 180);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 45);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 150, 180);
            ModifierContain mc_text1 = new ModifierContain(openTextImage5);
            openTextImage5.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage5);
        }

        public void Play6()
        {
            Animation open2 = new Animation("images/opening/open", new Rectangle(2, 57, 162, 146), new Point(162, 146));

            _openImage2 = new Image(open2);
            //_openImage2.Scale = 0.8f;
            _openImage2.Position = new Vector2(400, 240);
            PositionModifier m = new PositionModifier(_openImage2.Position, _openImage2.Position + new Vector2(75, 110), 120, 210);
            AlphaModifier a = new AlphaModifier(0.8f, 1, 1, 60);
            //AlphaModifier a2 = new AlphaModifier(1, 0, 150, 170);//
            ModifierContain mc = new ModifierContain(_openImage2);
            _openImage2.ModifierContainer = mc;
            m.OnModifyAction += ScaleChange;
            m.ModifyDoneAction += Play7;
            //mc.AddModifier(a2);
            mc.AddModifier(a);
            mc.AddModifier(m);
            _controls.Add(_openImage2);

            Animation open1 =
                new Animation("images/opening/open", new Rectangle(278, 2, 162, 146), new Point(162, 146));
            _openImage1 = new Image(open1);
            //_openImage1.Scale = 0.8f;
            _openImage1.Position = new Vector2(400, 240);
            m = new PositionModifier(_openImage1.Position, _openImage1.Position + new Vector2(75, 110), 120, 210);
            AlphaModifier a3 = new AlphaModifier(0, 1, 1, 60);
            AlphaModifier a4 = new AlphaModifier(1, 0.2f, 60, 100);
            AlphaModifier a5 = new AlphaModifier(0.2f, 0.8f, 100, 130);
            AlphaModifier a6 = new AlphaModifier(0.8f, 0.2f, 130, 150);
            AlphaModifier a7 = new AlphaModifier(0.2f, 0.6f, 150, 170);
            AlphaModifier a8 = new AlphaModifier(0.6f, 0.4f, 170, 210);
            mc = new ModifierContain(_openImage1);
            _openImage1.ModifierContainer = mc;
            mc.AddModifier(a3);
            mc.AddModifier(a4);
            mc.AddModifier(a5);
            mc.AddModifier(a6);
            mc.AddModifier(a7);
            mc.AddModifier(a8);
            mc.AddModifier(m);

            _controls.Add(_openImage1);


            Animation openText6 = new Animation("images/opening/text", new Rectangle(2, 289, 689, 84), new Point(689, 84));
            Image openTextImage6 = new Image(openText6);
            openTextImage6.Position = new Vector2(400, 100);
            PositionModifier p_text1 = new PositionModifier(openTextImage6.Position, openTextImage6.Position + new Vector2(0, 0), 0, 180);
            AlphaModifier a_text1 = new AlphaModifier(0, 1, 1, 60);
            AlphaModifier a_text2 = new AlphaModifier(1, 0, 120, 180);
            ModifierContain mc_text1 = new ModifierContain(openTextImage6);
            openTextImage6.ModifierContainer = mc_text1;
            mc_text1.AddModifier(p_text1);
            mc_text1.AddModifier(a_text1);
            mc_text1.AddModifier(a_text2);

            _controls.Add(openTextImage6);
        }

        public void ScaleChange()
        {
            tempCount++;
            if (tempCount > 10 && tempCount<60) 
            {
                _openImage2.Scale -= 0.01f;
                _openImage1.Scale -= 0.01f;
            }
 
        }

        public void Play7()
        {
            Animation open2 = new Animation("images/opening/open", new Rectangle(166, 150, 274, 53), new Point(274, 53));
            //open2.Scale = 2;

            Image openImage2 = new Image(open2);
            openImage2.Scale = 1.5f;
            openImage2.Position = new Vector2(400, 150);
            PositionModifier m = new PositionModifier(openImage2.Position, openImage2.Position + new Vector2(0, 0), 0, 180);
            AlphaModifier a = new AlphaModifier(0.8f, 1, 1, 60);
            //AlphaModifier a2 = new AlphaModifier(1, 0, 150, 180);
            ModifierContain mc = new ModifierContain(openImage2);
            openImage2.ModifierContainer = mc;
            m.ModifyDoneAction += Play8;
            //mc.AddModifier(a2);

            
            mc.AddModifier(a);
            mc.AddModifier(m);

        
            _controls.Add(openImage2);

            Animation open1 =
                new Animation("images/opening/open", new Rectangle(2, 2, 274, 53), new Point(274, 53));
            //open1.Scale = 2;
            Image openImage1 = new Image(open1);
            openImage1.Scale = 1.5f;
            openImage1.Position = new Vector2(400, 150);
            mc = new ModifierContain(openImage1);
            openImage1.ModifierContainer = mc;
            AlphaModifier a3 = new AlphaModifier(0, 1f, 1, 120);
            //AlphaModifier a4 = new AlphaModifier(1, 0, 75, 100);
            //m.ModifyDoneAction += m.UnRegister;
            mc.AddModifier(a3);
            //mc.AddModifier(a4);

            _controls.Add(openImage1);


            Animation open3 =
                new Animation("images/player/player",new Rectangle(48, 0, 336, 60), new Point(48, 60));
            open3.DeltaTime = 80;
            Image openImage3 = new Image(open3);
            openImage3.Scale = 2f;
            openImage3.Position = new Vector2(240, 320);
            PositionModifier m2 = new PositionModifier(openImage3.Position, openImage3.Position + new Vector2(140, 0), 0, 180);
            AlphaModifier a4 = new AlphaModifier(0, 1, 1, 120);
            mc = new ModifierContain(openImage3);
            openImage3.ModifierContainer = mc;
            mc.AddModifier(a4);
            mc.AddModifier(m2);

            _controls.Add(openImage3);
        }

        public void Play8()
        {
            Transition tra = new DefauldTransition(0, 1, 0, 90);
            tra.Register(this);
            tra.TransitionDoneAction += Play9;
        }

        public void Play9()
        {
            GameClass game1 = (GameClass)this.Game;
            game1.CurrentStage = (new MenuStage(game1)).Clone();
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            TouchCollection TouchPoints = TouchPanel.GetState();
            TouchLocationState state = TouchLocationState.Invalid;
            foreach (TouchLocation touchPointEvent in TouchPoints)
            {
                state = touchPointEvent.State;
                if (state == TouchLocationState.Moved) {
                    Play8();
                }
            }
        }

        public override void PressedBackButton()
        {
           // base.MenuState();
            GameClass.IsExit = true;
        }

        public override Stage Clone()
        {
            OPeningStage stage = new OPeningStage(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
